package com.jeesite.modules.utils;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.session.Session;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 系统工具类，将一些重复的工具代码写在此处
 * @author: ciye
 * @date: 2022/3/8 17:06
 */
public class OfficeUtil {
    /**
     * 返回当前用户的机构信息
     * @return
     */
    public static Office getCurrentOffice() {
        Session session = UserUtils.getSession();
        Office office = new Office();
        if (session.getAttribute("officeCode") != null) {
            office.setOfficeCode(session.getAttribute("officeCode").toString());
            office.setOfficeName(session.getAttribute("officeName").toString());
        } else {
            office = EmpUtils.getEmployee().getOffice();
        }
        return office;
    }

    /**
     * 获取年份列表
     * @param currentYear
     * @return
     */
    public static List getYearList(Integer currentYear) {
        // 检查参数是否为空
        if (currentYear == null) {
            currentYear = Integer.parseInt(DateUtils.getYear());
        }

        // 自动获取年度，近几年前台可自由配置
        Integer yearNumber = Integer.parseInt(DictUtils.getDictValue("dc_task_year", "year", null));
        List yearList = new ArrayList<>();

        for (int j = yearNumber; j >= 1; j--) {
            Map<String, Integer> yearMap = new HashMap<>();
            yearMap.put("yearLabel", currentYear + j);
            yearMap.put("yearValue", currentYear + j);
            yearList.add(yearMap);
        }
        for (int i = 0; i <= yearNumber; i++) {
            Map<String, Integer> yearMap = new HashMap<>();
            yearMap.put("yearLabel", currentYear);
            yearMap.put("yearValue", currentYear);
            yearList.add(yearMap);
            currentYear = currentYear - 1;
        }

        return yearList;
    }

    /**
     * 通过任务的反馈时间要求，和反馈频次，拼接完整的反馈要求
     * @param feedbackWay 反馈时间要求：1.按季度、2.按月、3.按周、4.单次反馈
     * @param feedbackRate 反馈频率
     * @return
     */
    public static String getTaskRequire(String feedbackWay, String feedbackRate) {
        // 定义返回字段
        String result = "";
        // 反馈时间要求和反馈频率不为空
        if (StringUtils.isBlank(feedbackWay) && StringUtils.isBlank(feedbackRate)) {
            result = "未获取任务反馈要求";
        } else {
            // 获取反馈时间要求的名称
            String feedbackWayName = DictUtils.getDictLabel("dc_feedback_circle_type", feedbackWay, "");
            // 名称获取为空，表示字典值发生了变动
            if (StringUtils.isBlank(feedbackWayName)) {
                result = "反馈时间要求设置错误";
            } else {
                // 按照反馈时间要求，拼接反馈要求
                try {
                    if (StringUtils.equals("1", feedbackWay) && StringUtils.isNotBlank(feedbackRate)) {
                        // 按季度
                        String[] months = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
                        result = feedbackWayName + " - 每季度首月" + months[Integer.parseInt(feedbackRate) - 1] + "号之前反馈";
                    } else {
                        // feedbackRate允许为空，此时只展示按季度反馈
                        result = feedbackWayName+ "反馈";
                    }
                    if (StringUtils.equals("2", feedbackWay) && StringUtils.isNotBlank(feedbackRate)) {
                        // 按月
                        String[] months = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
                        result = feedbackWayName + " - 每月" + months[Integer.parseInt(feedbackRate) - 1] + "号之前反馈";
                    } else {
                        // feedbackRate允许为空，此时只展示按月反馈
                        result = feedbackWayName+ "反馈";
                    }
                    if (StringUtils.equals("3", feedbackWay) && StringUtils.isNotBlank(feedbackRate)) {
                        // 按周
                        String[] weeks = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
                        result = feedbackWayName + " - 每" + weeks[Integer.parseInt(feedbackRate) - 1] + "之前反馈";
                    } else {
                        // feedbackRate允许为空，此时只展示按周反馈
                        result = feedbackWayName+ "反馈";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result = "反馈频率设置错误";
                }
                if (StringUtils.equals("4", feedbackWay)) {
                    // 单次反馈
                    result = feedbackWayName;
                }
            }

        }
        return result;
    }
}
